package com.loongtech.bi.action.flow;


import com.loongtech.bi.action.Base2BiSearchAction;
import com.loongtech.bi.manager.flow.HadoopQueryManager;
import com.loongtech.bi.manager.system.SysLogManager;
import com.loongtech.bi.support.Session;
import com.loongtech.core.util.Time;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map.Entry;

@Controller
@Scope("view")
public class FightRankingListAction extends Base2BiSearchAction {
	private static final long serialVersionUID = 1L;
    private Date dateEnd = new Date(Time.getBeginningOfDay(Time.currentTime() - Time.DAY));
    private List<Object[]> result;  // 结果List

    @Resource
    HadoopQueryManager hadoopQueryManager;

    @Resource
    private SysLogManager sysLogManager;
    @Resource
    private Session session;

    @PostConstruct
    public void init() {
        result = new ArrayList<>();
    }

    @Override
    public void search() {
        sysLogManager.addLog(session.getUserName(), "查询流水查询—战斗力排行");
        FacesContext context = FacesContext.getCurrentInstance();

        // 开始结束时间不能大于当前时间60天
        if (Time.getInterval(new Date(), dateStart) > 30 || Time.getInterval(new Date(), dateEnd) > 30) {
            context.addMessage(null, new FacesMessage("提示", "开始结束时间不能小于当前时间30天!"));
            return;
        }
        if (Time.getInterval(dateStart, dateEnd) < 0) {
            context.addMessage(null, new FacesMessage("提示", "开始时间不能大于结束时间!"));
            return;
        }
        if (HadoopQueryManager.VISIT_QUEUE.offer("FightRankingListAction")) {
            System.out.println("VISIT_QUEUE offer FightRankingListAction ok!");

            StringBuilder sql = new StringBuilder();
            sql.append("select b.platform,b.channel,b.serverid,b.userid,b.rolename,b.roleid,a.fightvalue, a.logtime from (select aa.roleid,aa.logtime,aa.dt,aa.fightvalue from fightvaluerank  aa,(select roleid,max(logtime) logtime from fightvaluerank where");
            sql.append(" dt <= '");
            sql.append(Time.getDateStr(dateEnd));
            sql.append("' group by roleid) bb where ");
            sql.append("aa.dt<='");
            sql.append(Time.getDateStr(dateEnd));
            sql.append(" ' and aa.roleid = bb.roleid and aa.logtime = bb.logtime) as a left join (select cc.platform,cc.channel,cc.serverid,cc.userid,cc.rolename,cc.roleid from rolecreate  cc,(select roleid,max(logtime) logtime from rolecreate where ");
            sql.append(" dt <= '");
            sql.append(Time.getDateStr(dateEnd));
            sql.append("' group by roleid) dd where");
            sql.append(" cc.dt <= '");
            sql.append(Time.getDateStr(dateEnd));
            sql.append(" ' and cc.roleid = dd.roleid and cc.logtime = dd.logtime) as b on a.roleid = b.roleid ");
            if (!"0".equals(queryServer)) {
                sql.append("where b.serverid = ");
                sql.append(queryServer);
            }
            sql.append(" order by a.fightvalue desc limit 100;");
            System.out.println(sql.toString());
            Entry<List<String>, List<Object[]>> ret = hadoopQueryManager.executeSQL(sql.toString());
            if (null == ret || null == ret.getKey() || ret.getKey().isEmpty() || null == ret.getValue() || ret.getValue().size() <= 0) {
                result.clear();
                context.addMessage(null, new FacesMessage("提示", "查询结果为空!"));
                return;
            }
            result = ret.getValue();
        } else {
            System.out.println("VISIT_QUEUE offer FightRankingListAction failed!");
            context.addMessage(null, new FacesMessage("提示", "当前系统正被访问，为保证服务器稳定，请稍后再试"));
        }
    }

    @Override
    protected String csvContent() {
        return null;
    }

    @Override
    protected String csvFileName() {
        return null;
    }

    public List<Object[]> getResult() {
        return result;
    }

    public void setResult(List<Object[]> result) {
        this.result = result;
    }

    @Override
    public Date getDateEnd() {
        return dateEnd;
    }

    @Override
    public void setDateEnd(Date dateEnd) {
        this.dateEnd = dateEnd;
    }
}
